<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
		<link rel="stylesheet" href="css/style.css" type="text/css">
		<title>用户注册</title>
	</head>
	<body>

<?php
	//将用户信息存入数据库
	include_once "connection.php";
	$user_name = $_POST["user_name"];
	$user_password = $_POST["user_password"];
	$user_email = $_POST["user_email"];
	$message = array();

	if(empty($user_name) || empty($user_password) || empty($user_email)) {
		echo "请填写完整的用户信息";
		exit();
	}
	
	$sql = "insert into users(user_name, user_password, user_email, user_level, register_date) 
		values(?, ?, ?, ?, ?)";
	try{
		$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$dbh->beginTransaction();

		$result = $dbh->prepare($sql);		/* 使用PDO::prepare()防止SQL注入 */
		$result->execute(array($user_name, md5($user_password), $user_email, '1', date('Y-m-d H:i:s')));

		$message[0] = "恭喜，注册成功";
		$message[1] = "您的ID是：";
		$message[2] = $dbh->lastInsertId();
		$message[3] = "请妥善保管";
		$message[4] = '进入<a href="login.html">登录</a>界面';
		$dbh->commit();

	}
	catch(Exception $e){
		$dbh->rollBack();
		$message[0] = "注册失败";
		$message[1] = "错误信息:";
		$message[2] = $e->getMessage();
		$message[3] = "请重新尝试";
		$message[4] = '返回<a href="register.html">注册</a>界面';
	}

?>
	<div class="register">
		<div class= "process">
<?php
	foreach($message as $info) {
		echo "$info<br>";
	}
?>
		</div>
	</div>
	</body>
</html>